<?php
/**
 * Created by PhpStorm.
 * User: yandian
 * Date: 16/10/26
 * Time: 12:08
 */

namespace app\common\util;


use think\Config;

class EmailHelper
{
    public static function sendEmail($to, $title, $content, $attaches = [])
    {
        Vendor('PHPMailer.PHPMailerAutoload');
        $mail = new \PHPMailer(); //实例化
        $mail->isSMTP();
        $mail->Host = Config::get('email.MAIL_HOST');
        $mail->SMTPAuth = Config::get('email.MAIL_SMTPAUTH'); //启用smtp认证
        $mail->Username = Config::get('email.MAIL_USERNAME'); //你的邮箱名
        $mail->Password = Config::get('email.MAIL_PASSWORD'); //邮箱密码
        $mail->From = Config::get('email.MAIL_FROM'); //发件人地址（也就是你的邮箱地址）
        $mail->FromName = Config::get('email.MAIL_FROMNAME'); //发件人姓名
        $mail->AddAddress($to, "尊敬的客户");
        $mail->WordWrap = 50; //设置每行字符长度
        $mail->IsHTML(Config::get('email.MAIL_ISHTML')); // 是否HTML格式邮件
        $mail->CharSet = Config::get('email.MAIL_CHARSET'); //设置邮件编码
        $mail->Subject = $title; //邮件主题
        $mail->Body = $content; //邮件内容
        $mail->AltBody = '此邮件来自于趣味筹'; //邮件正文不支持HTML的备用显示
        if (!empty($attaches)) {
            foreach ($attaches as $attach) {
                if (count($attach) == 2) {
                    $mail->addAttachment($attach[0], $attach[1]);
                }
            }
        }
        return $mail->Send();
    }
}